define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'kmh/order/index' + location.search,
                    add_url: 'kmh/order/add',
                    edit_url: 'kmh/order/edit',
                    del_url: 'kmh/order/del',
                    send_url: 'kmh/order/send',
                    multi_url: 'kmh/order/multi',
                    import_url: 'kmh/order/import',
                    table: 'new_order',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='voucher_id']", form).addClass("selectpage").data("source", "kmh/voucher/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");
                $("input[name='business_id']", form).addClass("selectpage").data("source", "business/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");
                $("input[name='staff_id']", form).addClass("selectpage").data("source", "staff/index").data("primaryKey", "id").data("field", "mobile").data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'type', title: __('Type'),formatter: function (value) {
                                switch (value) {
                                    case 1:
                                        return '立减券';
                                    case 2:
                                        return '实物';
                                }
                            },searchList: {1:'立减券',2:'实物'}},
                        {field: 'order_no', title: __('Order_no'), operate: '='},
                        {field: 'voucher_id', title: __('Voucher_id'),visible:false},
                        {field: 'voucher.name', title: __('礼品'), operate: false,},
                        {field: 'voucher.money_hui', title: __('金额'), operate: false,},
                        {field: 'number', title: __('数量'), operate: false,},
                       /* {field: 'voucher_stock_id', title: __('Voucher_stock_id'), operate: 'LIKE'},
                        {field: 'user_coupon_id', title: __('User_coupon_id'), operate: 'LIKE'},*/

                       /* {field: 'admin_id', title: __('Admin_id')},*/
                        {field: 'user_name', title: __('User_name'), operate: 'LIKE'},
                        {field: 'user_phone', title: __('User_phone'), operate: 'LIKE'},
                        {field: 'user_money', title: __('User_money'), operate: false },
                        {field: 'pay_date', title: '缴费时间', operate: false },
                        {field: 'room_no', title: '房号', operate: false },
                        {field: 'branch_name', title: __('支行'), operate: 'like' },
                        {field: 'gyh', title: __('柜员号'), operate: '=' },
                        {field: 'time_limit', title: __('业务期限'), operate: false },
                        {field: 'staff_id', title: __('Staff_id'),visible:false},
                        {field: 'staff.name', title: __('操作员工姓名'),operate: false},
                        {field: 'staff.wkno', title: __('操作员工号'),operate: false},
                        {field: 'business_id', title: __('Business_id'),visible:false},
                        {field: 'business.name', title: __('业务'),operate: false},
                        {field: 'extend1', title: __('客户类型'),formatter: function (value) {
                                switch (value) {
                                    case 1:
                                        return '行内';
                                    case 2:
                                        return '行外';
                                }
                            },searchList: {1:'行内',2:'行外'}},
                        {field: 'image', title: __('凭证'),operate: false,formatter:Table.api.formatter.image,events:Table.api.events.image},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                            {
                                name: 'send',
                                text: __('发放'),
                                title: __('发放'),
                                classname: 'btn btn-xs btn-success btn-ajax',
                                icon: 'fa fa-check',
                                url: 'kmh/order/send',
                                confirm: '确认发放?',
                                success: function (data, ret) {
                                    Layer.alert(ret.msg);
                                    return false;
                                },
                                error: function (data, ret) {
                                    Layer.alert(ret.msg);
                                    return false;
                                },
                                visible: function (row) {
                                    return row.status === 0;
                                }
                            }
                        ]}
                    ]
                ]
            });
            // 自定义导出按钮
            $(document).on("click", ".btn-myexcel-export", function () {
                var options = table.bootstrapTable('getOptions');
                var search  = options.queryParams({});
                var filter  = search.filter;
                var op      = search.op;

                var page = table.bootstrapTable('getData'); // 获取页面数据
                let ids = [];
                let id_data = '';
                // 取出当前页面的id
                $.each(page,function(i,v){
                    ids.push(v.id)
                    if(i == 0){
                        id_data = v.id;
                    }else{
                        id_data += ','+v.id;
                    }
                });
                layer.confirm("请选择导出的选项", {
                    title: '导出数据',
                    btn: ['全部数据','当前页数据(' + ids.length + '条)'],
                    yes: function (index, layero) {
                        // 全部数据
                        Layer.close(index);
                        top.location.href="order/export?id_data=&"+'filter='+filter+'&op='+op;
                    },
                    btn2: function (index, layero) {
                        // 当前页面数据
                        top.location.href="order/export?id_data="+id_data+'&'+'filter='+filter+'&op='+op;
                    }
                });
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});